﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;

namespace ScratchCard.AppCode
{
    public class PrizeSettingModel
    {
        private static object _lock = new object();

        private static string file = "prize.json";
        private static PrizeSettingModel _instance ;


        public PrizeSettingModel()
        {
            
        }


        public PrizeSettingModel(string starttime, string endtime, string introductions, string interpretation, string awards)
        {
            StartTime = DateTime.Parse( starttime).ToString("yyyy-MM-dd HH:mm:ss");
            EndTime = DateTime.Parse(endtime).ToString("yyyy-MM-dd HH:mm:ss");
            Introductions = new List<string>(introductions.Split(','));
            Interpretation = interpretation;
            Awards = JsonHelper.DeserializeJsonToList<AwardModel>(awards);
        }

        public void SetDefaultSetting()
        {
            StartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            EndTime = DateTime.Now.AddHours(1).ToString("yyyy-MM-dd HH:mm:ss");
            Introductions = new List<string>()
            {
                "亲，酷居刮奖区中有乾坤，祝您好运！", 
                "活动地点：深圳会展中心4号馆C111-115", 
                "活动时间：2015.06.26-06.27"
            };
            Interpretation = "本活动最终解释权归酷居科技所有";
            Awards = new List<AwardModel>()
            {
                new AwardModel()
                {
                    PrizeLevel = LockBoxex.Prize.First,
                    Prize = "一等奖",
                    Description = "一等奖：智能家居套装",
                    Price = "价值2888元",
                    Count = 0,
                }, 
                new AwardModel()
                {
                    PrizeLevel = LockBoxex.Prize.Second,
                    Prize = "二等奖",
                    Description = "二等奖：充电宝",
                    Price = "价值250元",
                    Count = 0,
                },
                new AwardModel()
                {
                    PrizeLevel = LockBoxex.Prize.Third,
                    Prize = "三等奖",
                    Description = "三等奖：迷你充电器",
                    Price = "价值100元",
                    Count = 0,
                },
                new AwardModel()
                {
                    PrizeLevel = LockBoxex.Prize.Forth,
                    Prize = "四等奖",
                    Description = "四等奖：迷你USB高速数据线",
                    Price = "价值38元",
                    Count = 0,
                },
            };
        }

        public static PrizeSettingModel Create()
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    var appPath = System.AppDomain.CurrentDomain.BaseDirectory;

                    var path = Path.Combine(appPath, "PrizeSetting");

                    var filename = Path.Combine(path, file);

                    if (File.Exists(filename))
                    {
                        var json = File.ReadAllText(filename);
                        _instance = JsonHelper.DeserializeJsonToObject<PrizeSettingModel>(json);
                    }
                    else
                    {
                        _instance = new PrizeSettingModel();
                        _instance.SetDefaultSetting();
                    }
                  
                }
                return _instance;
            }
        }


        public string StartTime { get; set; }
        public string EndTime { get; set; }

        public List<string> Introductions { get; set; }

        public string Interpretation { get; set; }

        public List<AwardModel> Awards { get; set; }

        internal void Save()
        {
            lock (_lock)
            {
                var json = JsonHelper.SerializeObject(this);

                var appPath = System.AppDomain.CurrentDomain.BaseDirectory;

                var path = Path.Combine(appPath, "PrizeSetting");

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                File.WriteAllText(Path.Combine(path, file), json, System.Text.Encoding.GetEncoding("utf-8"));
                _instance = null;
                _instance = Create();

                LuckyBox.Reset(_instance);
            }
        }
    }

    public class AwardModel
    {
        public LockBoxex.Prize PrizeLevel { get; set; }
        public string Prize { get; set; }

        public string Description { get; set; }

        public string Price { get; set; }

        public int Count { get; set; }

        public float Random { get; set; }
        public int IssueCount { get; set; }
    }
}